﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Imaging;
using Tao.OpenGl;

namespace CutAways.Modeling
{
	public class TextureLoader
	{
		/// <summary>
		/// nacte texturu ze souboru a vrati jeji ID
		/// </summary>
		/// <param name="filename">cesta k souboru s texturou</param>
		/// <returns>id textury</returns>
		public int LoadTextureFromFile(string filename)
		{
			Bitmap image = new Bitmap(filename);
			image.RotateFlip(RotateFlipType.RotateNoneFlipY);
			BitmapData bitmapdata;
			Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);

			bitmapdata = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

			int textureID;

			Gl.glGenTextures(1, out textureID);
			Gl.glBindTexture(Gl.GL_TEXTURE_2D, textureID);
			Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8, image.Width, image.Height, 0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
			Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);		// Linear Filtering
			Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);		// Linear Filtering

			image.UnlockBits(bitmapdata);
			image.Dispose();

			return textureID;
		}
	}
}
